package com.example.test;

import controller.Controlador;
import domain.Sesion;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
	Button botonLogin;
	EditText textUser;
	EditText textPassword;
	ProgressDialog pd;
	TextView registrateAdviceTextView;
	
	
	String user;
	String password;
	String status;
	

	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        botonLogin = (Button) findViewById(R.id.botonLogin);
        textUser = (EditText) findViewById(R.id.usernameText);
        textPassword = (EditText) findViewById(R.id.passwordText);
        
        /*
         * Clase hilo
         */
        
        registrateAdviceTextView = (TextView) findViewById(R.id.registrateAdviceTextView);
        registrateAdviceTextView.setTextColor(Color.BLUE);
        
        registrateAdviceTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent registro = new Intent(MainActivity.this,RegistroActivity.class);
        		startActivity(registro);
			}
		});
	
        class loginHilo extends AsyncTask<Void,Void,Void>{
    		@Override
    		protected void onPostExecute(Void result) {
    			pd.dismiss();
    			
    			if(status.equals("false")){
            		Toast error = Toast.makeText(
            				getApplicationContext(),
            				"Hubo un error en el login: "+status, 
            				Toast.LENGTH_LONG);
            		error.show();
            	} else {
            		status = "false";
            		Intent logeado = new Intent(MainActivity.this,LogeadoActivity.class);
            		startActivity(logeado);
            		domain.Sesion.username = user;
            		finish();	

            	}
    			super.onPostExecute(result);
    		}
    		
    		
			@Override
			protected void onPreExecute() {
				pd = ProgressDialog.show(MainActivity.this, "Ingresando", "Por favor espere");
				super.onPreExecute();
			}

			@Override
			protected Void doInBackground(Void... arg0) {
				status = Controlador.login(user, password);
				
				return null;
			}
    		
			
    		
    	}
        
        botonLogin.setOnClickListener(new OnClickListener() {
            
        	
        	@Override
            public void onClick(View arg0) {
            	user = textUser.getText().toString();
            	password = textPassword.getText().toString();
            	new loginHilo().execute();
            	
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
